package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.entity.statistics.LoanAdsChannelDetail;
import com.tuanzi.loan.business.service.statistics.AdverChannelTypeUserService;
import com.tuanzi.loan.business.service.statistics.LoanAdsChannelDetailService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateAdvertiseConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateAdvertiseConfigListResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.collaborate.CollaborateAdvertiseConfigListLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 10:44 2018/9/18
 * Description:
 */

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateAdvertiseConfigListBean extends JSFListingSessionBean<CollaborateAdvertiseConfigListRequest, CollaborateAdvertiseConfigListResponse> {

    private static final long serialVersionUID = 3241280987319303215L;

    @Autowired
    private AdverChannelTypeUserService adverChannelTypeUserService;
    @Autowired
    private LoanAdsChannelDetailService loanAdsChannelDetailService;

    private List<LoanAdsChannelDetail> channelLists;

    @PostConstruct
    public void init(){
        initLazyDataModel();
    }

    protected String page() {
        return "/views/collaborate/collaborate-advertise-config-list.xhtml";
    }

    protected void initLazyDataModel(){
        $ = new CollaborateAdvertiseConfigListRequest();
        lazyDataModel = new CollaborateAdvertiseConfigListLazyDataModel($, session, adverChannelTypeUserService);
        initChannelLists();
    }
    protected void initChannelLists(){
        channelLists =  loanAdsChannelDetailService.findAllChannel();
    }

    public String add(){
        return redirect("/views/collaborate/collaborate-advertise-config-info.xhtml");
    }


    public void onRest(){
        initLazyDataModel();
    }

    public void remove(Long id){
        if(null == id){
            addWarnMessage("无法找到id");
            return;
        }

        AdverChannelTypeUser deleteInfo = adverChannelTypeUserService.findOne(id);
        if(Objects.nonNull(deleteInfo)){
            adverChannelTypeUserService.delete(id);
        }

        initLazyDataModel();
        addSuccessfullyMessage();
    }

    public String toInfo(Integer id){
        return redirect("/views/collaborate/collaborate-advertise-config-info.xhtml?id=" + id);
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.ADS_PLATFORM_TYPE);
    }

}
